/*++

Copyright (c) 2007  Microsoft Corporation 

Filename: 

    FrmImport.cs

Abstract: 

    Form to select various options for rack server virtualization 
     
--*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Reflection;
using System.Windows.Forms;
using AddIn.Windows.Forms;
using UtilityManager;
using ExcelManager;
using System.IO;


namespace RackServerManager
{
    public partial class FrmImport : AddIn.Windows.Forms.ExteriorWizardPage
    {
        private System.Windows.Forms.OpenFileDialog openFileDialog1;  

        /// <summary>
        /// Default Constructor
        /// </summary>
        public FrmImport()
        {
            // This call is required by the Windows Form Designer
            InitializeComponent();
            this.TitleLabel.Text   = Global.GetResourceString("FormImportWelcome_Label");
            this.rbSources2.Text   = Global.GetResourceString("FormImportOption2_Radio"); 
            this.rbSources1.Text   = Global.GetResourceString("FormImportOption1_Radio");
            this.rbSources3.Text   = Global.GetResourceString("FormImportOption3_Radio");
            this.lblDescription.Text = Global.GetResourceString("FormImportDesc");
        }

        /// <summary>
        /// Enables Next Button
        /// </summary>
        /// <returns>
        /// True if enables next button 
        ///</returns>
        protected override bool OnSetActive()
        {
            if (!base.OnSetActive())
                return false;

            if (rbSources1.Checked == true)
            {
                lblDescText.Text = Global.GetResourceString("FormImportExcelDesc");  
            }
            else if (rbSources2.Checked == true)
            {
                lblDescText.Text = Global.GetResourceString("FormImportTemplateDesc");  
            }
            else
            {
                lblDescText.Text = Global.GetResourceString("FormImportWizardDesc");   
            }

            // Check the option selected by user on previous run
            if (Global.IPOption == Global.InputDataOption.Wizard)
            {
                rbSources3.Checked = true;
                // Enable only the Next button on this page    
                Wizard.SetWizardButtons(WizardButtons.Next);
            }
            else if (Global.IPOption == Global.InputDataOption.Excel)
            {
                rbSources1.Checked = true;
                // Enable only the Next button on this page    
                Wizard.SetWizardButtons(WizardButtons.Next);
            }
            else
            {
                rbSources2.Checked = true;
                // Enable only the Finish button on this page    
                Wizard.SetWizardButtons(WizardButtons.Finish);
            }

            
            return true;
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.rbSources1 = new System.Windows.Forms.RadioButton();
            this.lblDescription = new System.Windows.Forms.Label();
            this.gbDescription = new System.Windows.Forms.GroupBox();
            this.lblDescText = new System.Windows.Forms.Label();
            this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
            this.rbSources3 = new System.Windows.Forms.RadioButton();
            this.rbSources2 = new System.Windows.Forms.RadioButton();
            this.SuspendLayout();
            // 
            // openFileDialog1
            // 
            this.openFileDialog1.FileName = "openFileDialog1";
            // 
            // rbSources1
            // 
            this.rbSources1.AutoSize = true;
            this.rbSources1.Location = new System.Drawing.Point(168, 79);
            this.rbSources1.Name = "rbSources1";
            this.rbSources1.Size = new System.Drawing.Size(14, 13);
            this.rbSources1.TabIndex = 4;
            this.rbSources1.TabStop = true;
            this.rbSources1.UseVisualStyleBackColor = true;
            this.rbSources1.CheckedChanged += new System.EventHandler(this.rbSources1_CheckedChanged);
            // 
            // lblDescription
            // 
            this.lblDescription.AutoSize = true;
            this.lblDescription.ForeColor = System.Drawing.SystemColors.Highlight;
            this.lblDescription.Location = new System.Drawing.Point(154, 166);
            this.lblDescription.Name = "lblDescription";
            this.lblDescription.Size = new System.Drawing.Size(60, 13);
            this.lblDescription.TabIndex = 21;            
            // 
            // gbDescription
            // 
            this.gbDescription.Location = new System.Drawing.Point(214, 173);
            this.gbDescription.Name = "gbDescription";
            this.gbDescription.Size = new System.Drawing.Size(270, 2);
            this.gbDescription.TabIndex = 22;
            this.gbDescription.TabStop = false;
            // 
            // lblDescText
            // 
            this.lblDescText.Location = new System.Drawing.Point(165, 188);
            this.lblDescText.Name = "lblDescText";
            this.lblDescText.Size = new System.Drawing.Size(300, 35);
            this.lblDescText.TabIndex = 23;
            // 
            // rbSources3
            // 
            this.rbSources3.AutoSize = true;
            this.rbSources3.Location = new System.Drawing.Point(168, 129);
            this.rbSources3.Name = "rbSources3";
            this.rbSources3.Size = new System.Drawing.Size(14, 13);
            this.rbSources3.TabIndex = 24;
            this.rbSources3.TabStop = true;
            this.rbSources3.UseVisualStyleBackColor = true;
            // 
            // rbSources2
            // 
            this.rbSources2.AutoSize = true;
            this.rbSources2.Location = new System.Drawing.Point(168, 104);
            this.rbSources2.Name = "rbSources2";
            this.rbSources2.Size = new System.Drawing.Size(14, 13);
            this.rbSources2.TabIndex = 5;
            this.rbSources2.TabStop = true;
            this.rbSources2.UseVisualStyleBackColor = true;
            this.rbSources2.CheckedChanged += new System.EventHandler(this.rbSources2_CheckedChanged);
            // 
            // FrmImport
            // 
            this.Controls.Add(this.rbSources3);
            this.Controls.Add(this.lblDescText);
            this.Controls.Add(this.gbDescription);
            this.Controls.Add(this.lblDescription);
            this.Controls.Add(this.rbSources1);
            this.Controls.Add(this.rbSources2);
            this.Name = "FrmImport";
            this.Load += new System.EventHandler(this.frmImport_Load);
            this.Controls.SetChildIndex(this.rbSources2, 0);
            this.Controls.SetChildIndex(this.rbSources1, 0);
            this.Controls.SetChildIndex(this.lblDescription, 0);
            this.Controls.SetChildIndex(this.gbDescription, 0);
            this.Controls.SetChildIndex(this.lblDescText, 0);
            this.Controls.SetChildIndex(this.rbSources3, 0);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion    
        
        /// <summary>
        /// Actions to be performed based on selected option
        /// </summary>
        /// <returns></returns>
        protected override string OnWizardNext()
        {
            try
            {                   
                if (rbSources1.Checked == true)
                {
                    if (!ExcelServiceManager.ExcelInstalled())
                    {
                        MessageBox.Show(Global.GetResourceString("Excel_Install"),
                            Global.GetResourceString("VirtualRack_MsgBox_Title"),
                            MessageBoxButtons.OK, MessageBoxIcon.Error,
                            MessageBoxDefaultButton.Button1);
                        return "FrmImport";
                    }
                    else
                    {
                        // Get a excel file name from user
                        this.openFileDialog1.FileName = "";
                        this.openFileDialog1.InitialDirectory = Global.AppPath();
                        this.openFileDialog1.CheckFileExists = true;
                        this.openFileDialog1.CheckPathExists = true;
                        this.openFileDialog1.Multiselect = false;
                        this.openFileDialog1.Filter = Global.GetResourceString("Excel_FileDialog_Filter");
                        DialogResult ExcelFileSelect = this.openFileDialog1.ShowDialog();
                        Global.IPOption = Global.InputDataOption.Excel;

                        if (ExcelFileSelect == DialogResult.OK)
                        {
                            Global.ExcelFileName = this.openFileDialog1.FileName;                            
                            Global.ExcelSheetsName = ExcelServiceManager.GetExcelSheetNames(this.openFileDialog1.FileName);

                            if (Global.ExcelSheetsName == null)
                                return "FrmImport";

                            // Set comboboxes index of frmSelectWorksheet
                            switch (Global.ExcelSheetsName.Length)
                            {
                                case 1:
                                    Global.CmbColoIndex = 0;
                                    Global.CmbRackIndex = 0;
                                    Global.CmbServerIndex = 0;
                                    break;

                                case 2:
                                    Global.CmbColoIndex = 0;
                                    Global.CmbRackIndex = 1;
                                    Global.CmbServerIndex = 1;
                                    break;

                                default:
                                    Global.CmbColoIndex = 0;
                                    Global.CmbRackIndex = 1;
                                    Global.CmbServerIndex = 2;
                                    break;
                            }

                            if (Global.ExcelSheetsName != null)
                                return "FrmSelectWorksheet";
                            else
                                return "FrmImport";

                        }
                        else
                        {
                            return "FrmImport";
                        }
                    }
                }
                else
                {
                    Global.IPOption = Global.InputDataOption.Wizard;
                    return "FrmDataSummary";
                }
            }
            catch (RackException ex)
            {

                // To catch and display exceptions from sub functions
                RackException.HandleExceptions(Global.GetResourceString("Exception_MsgBox_Caption"),
                            Global.GetResourceString("Exception_UsrMsg"), ex.InnerException);

                return null;
            }
            catch (Exception ex)
            {
                // To catch exceptions from current functions
                RackException.HandleExceptions(Global.GetResourceString("Exception_MsgBox_Caption"),
                            Global.GetResourceString("Exception_UsrMsg"), ex);

                return null;
            }           
        }

        /// <summary>
        /// Actions to be performed based on selected option
        /// </summary>
        /// <returns></returns>
        protected override bool OnWizardFinish()
        {
            try
            {
                if (!ExcelServiceManager.ExcelInstalled())
                {
                    MessageBox.Show(Global.GetResourceString("Excel_Install"),
                        Global.GetResourceString("VirtualRack_MsgBox_Title"),
                        MessageBoxButtons.OK, MessageBoxIcon.Error,
                        MessageBoxDefaultButton.Button1);
                    return false;
                }
                else
                {
                        Global.IPOption = Global.InputDataOption.Template;

                        // Get a excel file name from user
                        this.saveFileDialog1.FileName = "";
                        this.saveFileDialog1.InitialDirectory = Global.AppPath();
                        this.saveFileDialog1.CheckFileExists = false;
                        this.saveFileDialog1.CheckPathExists = true;
                        this.saveFileDialog1.CreatePrompt = true;
                        this.saveFileDialog1.OverwritePrompt = true;
                        // this.saveFileDialog1.Multiselect = false;
                        this.saveFileDialog1.Filter = Global.GetResourceString("Excel_FileDialog_Filter");
                        DialogResult ExcelFileSelect = this.saveFileDialog1.ShowDialog();

                        if (ExcelFileSelect == DialogResult.OK)
                        {
                            FileInfo fileTemplate = new FileInfo(Global.AppPath() + "Sample Rack Server Virtualization.xlsx");
                            fileTemplate.CopyTo(saveFileDialog1.FileName, true);

                            MessageBox.Show(Global.GetResourceString("ExcelTemplateInformation_Text"),
                                Global.GetResourceString("MessageBoxCaption"),
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information,
                                MessageBoxDefaultButton.Button1);

                            ExcelServiceManager.OpenExcelFile(saveFileDialog1.FileName);

                            Global.ExcelSheetsName = ExcelServiceManager.GetExcelSheetNames(saveFileDialog1.FileName);

                            Global.ColoSelectQuery = @"SELECT [" + Global.ColoFieldColoName + "]," + 
                                "[" + Global.ColoFieldMaxUnits + "] FROM [" + Global.ColoDataRecordSetName +"$]";

                            Global.RackSelectQuery = @"SELECT [" + Global.RackFieldRackName + "], [" +
                                Global.RackFieldPowerRating + "], [" + Global.RackFieldMaxUnits + "], [" +
                                Global.RackFieldColoName + "] FROM ["  + Global.RackDataRecordSetName + "$]";

                            Global.ServerSelectQuery = @"SELECT [" + Global.ServerFieldNetworkName + "], [" + 
                                Global.ServerFieldPowerRating + "], [" + Global.ServerFieldRackSpace + "]," +
                                " [" + Global.ServerFieldRackName + "], [" + Global.ServerFieldColoName + "]," +
                                " '' as [" + Global.ServerFieldIPAdress + "], '' as [" + Global.ServerFieldOperatingSystem + "]," +
                                " '' as [" + Global.ServerFieldProcessor + "], '' as [" + Global.ServerFieldCpuUsage + "], " +
                                " '' as [" + Global.ServerFieldTotalMem + "], '' as [" + Global.ServerFieldAvailMem + "]," +
                                " '' as [" + Global.ServerFieldIsVirtual + "]" +
                                " FROM [" + Global.ServerDataRecordSetName + "$]";

                            Global.RecordSetCreated = true;
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
            }
            catch (IOException ex)
            {
                MessageBox.Show(ex.Message,
                        Global.GetResourceString("MessageBoxCaption"),
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Information,
                        MessageBoxDefaultButton.Button1);
                return false;
            }
            catch (RackException ex)
            {

                // To catch and display exceptions from sub functions
                RackException.HandleExceptions(Global.GetResourceString("Exception_MsgBox_Caption"),
                            Global.GetResourceString("Exception_UsrMsg"), ex.InnerException);

                return false;
            }
            catch (Exception ex)
            {
                // To catch exceptions from current functions
                RackException.HandleExceptions(Global.GetResourceString("Exception_MsgBox_Caption"),
                            Global.GetResourceString("Exception_UsrMsg"), ex);

                return false;
            }
        }    

        /// <summary>
        /// On Form Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmImport_Load(object sender, EventArgs e)
        {
            System.IO.Stream xsdResourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("RackServerManager.Images.data.bmp");
            this.WatermarkPicture.Image = System.Drawing.Image.FromStream(xsdResourceStream);
        }

        private void rbSources1_CheckedChanged(object sender, EventArgs e)
        {
            if (rbSources1.Checked == true)
            {
                lblDescText.Text = Global.GetResourceString("FormImportExcelDesc");                
                 // Enable only the Next button on this page    
                Wizard.SetWizardButtons(WizardButtons.Next);
            }
            else if (rbSources2.Checked == true)
            {
                lblDescText.Text = Global.GetResourceString("FormImportTemplateDesc"); 
                // Enable only the Finish button on this page    
                Wizard.SetWizardButtons(WizardButtons.Finish);
            }
            else
            {
                lblDescText.Text = Global.GetResourceString("FormImportWizardDesc"); 
                // Enable only the Next button on this page    
                Wizard.SetWizardButtons(WizardButtons.Next);
            }
        }

        private void rbSources2_CheckedChanged(object sender, EventArgs e)
        {
            if (rbSources1.Checked == true)
            {
                lblDescText.Text = Global.GetResourceString("FormImportExcelDesc"); 
                // Enable only the Next button on this page    
                Wizard.SetWizardButtons(WizardButtons.Next);
            }
            else if (rbSources2.Checked == true)
            {
                lblDescText.Text = Global.GetResourceString("FormImportTemplateDesc"); 
                // Enable only the Finish button on this page    
                Wizard.SetWizardButtons(WizardButtons.Finish);
            }
            else
            {
                lblDescText.Text = Global.GetResourceString("FormImportWizardDesc"); 
                // Enable only the Next button on this page    
                Wizard.SetWizardButtons(WizardButtons.Next);
            }            
        }
    }
}